package org.zn.note.designpattern;

/**
 * 状态模式（State Pattern）
 */
public class Pattern21_State {

    public static void main(String[] args) {
        Context context = new Context();

        StartState startState = new StartState();
        startState.doAction(context);

        System.out.println(context.getState().toString());

        StopState stopState = new StopState();
        stopState.doAction(context);

        System.out.println(context.getState().toString());
    }

    private interface State {
        void doAction(Context context);
    }

    private static class StartState implements State {

        public void doAction(Context context) {
            System.out.println("Player is in start state");
            context.setState(this);
        }

        public String toString() {
            return "Start State";
        }
    }

    private static class StopState implements State {

        public void doAction(Context context) {
            System.out.println("Player is in stop state");
            context.setState(this);
        }

        public String toString() {
            return "Stop State";
        }
    }

    private static class Context {
        private State state;

        Context() {
            state = null;
        }

        void setState(State state) {
            this.state = state;
        }

        State getState() {
            return state;
        }
    }
}
